package com.kcht.sylyg08.dao.impl;


import com.kcht.adp.common.log.Logger;
import com.kcht.sylyg08.dao.DataSourceBaseDao;
import com.kcht.sylyg08.dao.ITransSequencesDao;
import com.kcth.sylyg08.entity.TransSequences;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
  *
  * @Package:        com.kcht.sylyg08.dao.impl
  * @Description:     java类作用描述
  * @Author:         lenovo
  * @CreateDate:     2019/5/20 11:51
 */
@Singleton
@Startup
public class TransSequencesDao extends DataSourceBaseDao<TransSequences, Long> implements ITransSequencesDao {

    @Inject
    private Logger log;


    @Override
    public Long getTransnSequences(String transName, Date date) throws Exception {

        TransSequences transSequences = this.selectOneByJPQL("select a from TransSequences a where a.name=?", transName);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");

        Date currDate = null;
        currDate = df.parse(df1.format(date) + " 00:00:00");

        switch (transSequences.getCycle()){
            case 0:
                if((transSequences.getCurrentValue() < transSequences.getMaxValue()) ){
                    transSequences.setCurrentValue((transSequences.getCurrentValue() + transSequences.getIncrement()));
                }
                break;
            case 1:
                if((transSequences.getCurrentValue() >= transSequences.getMaxValue()) ){
                    transSequences.setCurrentValue(0);
                }
                transSequences.setCurrentValue((transSequences.getCurrentValue() + transSequences.getIncrement()));
                break;
            case 2:
                Date lasterDate = df.parse(df1.format(transSequences.getLasterTime()) + " 00:00:00");;
                if((currDate.equals(lasterDate))){
                    if((transSequences.getCurrentValue() >= transSequences.getMaxValue()) ){
                        transSequences.setCurrentValue(0);
                    }
                }else {
                    transSequences.setCurrentValue(0);
                    transSequences.setLasterTime(currDate);
                }
                transSequences.setCurrentValue((transSequences.getCurrentValue() + transSequences.getIncrement()));
                break;
            default:
                break;
        }

        return transSequences.getCurrentValue();
    }
}
